package com.servlet;

import com.bean.Admin;
import com.bean.Owner;
import com.service.LoginService;
import com.service.impl.LoginServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/LoginServlet/*")
public class LoginServlet extends HttpServlet {

    private LoginService loginService = new LoginServiceImpl();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String pathInfo = request.getPathInfo().substring(1);
        if("login".equals(pathInfo)){
            login(request,response);
        }else if("logout".equals(pathInfo)){
            logout(request,response);
        }else if("login1".equals(pathInfo)){
            login1(request,response);
        }else if ("logout1".equals(pathInfo)){
            logout1(request,response);
        }
    }

    private void logout1(HttpServletRequest request, HttpServletResponse response) {
        request.getSession().invalidate();
        try {
            response.sendRedirect(getServletContext().getContextPath()+"/login1.jsp");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void login1(HttpServletRequest request, HttpServletResponse response) {
        String id = request.getParameter("name");
        String passsword = request.getParameter("password");
        try {
            Owner owner = loginService.login1(id, passsword);
            if(owner==null){
                //登录失败:
                //打回登录页:
                request.setAttribute("errMsg","用户名或密码错误");
                request.getRequestDispatcher("/login1.jsp").forward(request,response);
            }else{
                //登录成功：
                //跳转到index首页：
                request.getSession().setAttribute("owner",owner);
                request.getSession().setAttribute("name",owner.getOwnerName());
                response.sendRedirect(getServletContext().getContextPath()+"/index2.jsp");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private void logout(HttpServletRequest request, HttpServletResponse response) {
        request.getSession().invalidate();
        try {
            response.sendRedirect(getServletContext().getContextPath()+"/login.jsp");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void login(HttpServletRequest request, HttpServletResponse response) {
        String name = request.getParameter("name");
        String passsword = request.getParameter("password");
        try {
            Admin admin = loginService.login(name, passsword);
            if(admin==null){
                //登录失败:
                //打回登录页:
                request.setAttribute("errMsg","用户名或密码错误");
                request.getRequestDispatcher("/login.jsp").forward(request,response);
            }else{
                //登录成功：
                //跳转到index首页：
                request.getSession().setAttribute("user",admin);
                response.sendRedirect(getServletContext().getContextPath()+"/index.jsp");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
